<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>防抖</title>
  </head>
  <body>
    <button>点我</button>
    <script>
      //  防抖：是一种优化高频触发事件的技术，核心思想是事件触发后等待一段时间再执行，如果在这段时间内再次触发，就重新计时

      function debounce(fn, wait = 500) {
        let timer = 0;
        return function (...args) {
          if (timer) {
            clearTimeout(timer);
          }
          timer = setTimeout(() => {
            fn.apply(this, args);
          }, wait);
        };
      }
      const btn = document.querySelector("button");
      btn.addEventListener("click", debounce(function (e) {
        console.log(888)
      }, 1000))
    </script>
  </body>
</html>
